package com.zlm.linked;

/**
 * @author zlm
 * @date 2025/4/29
 * 双向链表
 */
public class DoubleLinked<E> {

    private DoubleLinkedNode<E> head;

    private DoubleLinkedNode<E> tail;

    /**
     * 添加尾节点
     * @param value
     */
    public void addEndNode(E value) {
        DoubleLinkedNode<E> node = new DoubleLinkedNode<>(value);
        if (head == null && tail == null) {
            head = node;
            tail = node;
            return;
        }
        tail.next = node;
        node.pre = tail;
        tail = node;
    }

    /**
     * 添加头节点
     * @param value
     */
    public void addHeadNode(E value) {
        DoubleLinkedNode<E> node = new DoubleLinkedNode<>(value);
        if(head == null && tail == null){
            head = node;
            tail = node;
            return;
        }
        head.pre = node;
        node.next = head;
        head = node;

    }

    /**
     * 从前向后打印所有节点
     */
    public void printAll() {
        DoubleLinkedNode<E> current = head;
        while (current != null){
            System.out.print(current.value + "\t");
            current = current.next;
        }
        System.out.println();
    }

    /**
     * 从后向前打印所有节点
     */
    public void printAllReverse() {
        DoubleLinkedNode<E> current = tail;
        while (current != null){
            System.out.print(current.value + "\t");
            current = current.pre;
        }
        System.out.println();
    }

    public E deleteNode(E value) {
        DoubleLinkedNode<E> current = head;
        while (current != null){
            if(current.value.equals(value)){
                if(current.pre == null){
                    head = current.next;
                    current.next = null;
                    head.pre = null;
                }else if(current.next == null){
                    tail = current.pre;
                    current.pre = null;
                    tail.next = null;
                }else{
                    current.pre.next = current.next;
                    current.next.pre = current.pre;
                    current.pre = null;
                    current.next = null;
                }
                return current.value;
            }
            current = current.next;
        }
        return null;

    }


    static class DoubleLinkedNode<E>{
        private E value;

        private DoubleLinkedNode<E> pre;

        private DoubleLinkedNode<E> next;

        public DoubleLinkedNode(E value) {
            this.value = value;

        }
    }


    public static void main(String[] args) {

        DoubleLinked<Integer> root = new DoubleLinked<>();
        //root.addEndNode(1);
        //root.addEndNode(2);
        //root.addEndNode(3);
        //root.addEndNode(4);
        //root.addEndNode(5);

        root.addHeadNode(1);
        root.addHeadNode(2);
        root.addHeadNode(3);
        root.addHeadNode(4);
        root.addHeadNode(5);

        root.printAll();
        root.printAllReverse();
        System.out.println(root.deleteNode(10));
        root.printAll();

    }
}
